/**
 * 地址管理服务类
 * 用于获取地址列表、添加、编辑、删除地址等操作
 */

const request = require('../libs/request');

class AddressService {
	/**
	 * 获取地址列表（含分页）
	 * @param {Object} params 查询参数
	 * @param {number} params.page 页码，默认1
	 * @param {number} params.pageSize 每页数量，默认10
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回地址列表数据
	 */
	static async getAddressList(params = {}) {
		const { 
			page = 1, 
			pageSize = 10, 
			showLoading = false 
		} = params;

		try {
			const response = await request.authGet('/v1/address/index', {
				showLoading,
				data: {
					page,
					page_size: pageSize
				}
			});

			// 检查后端返回的状态码
			if (response.code === 1) {
				return {
					success: true,
					data: response.data,
					message: response.msg || '获取地址列表成功'
				};
			} else {
				return {
					success: false,
					data: null,
					message: response.msg || '获取地址列表失败'
				};
			}
		} catch (error) {
			console.error('获取地址列表失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '获取地址列表失败'
			};
		}
	}

	/**
	 * 获取地址详情
	 * @param {Object} params 查询参数
	 * @param {number} params.id 地址ID
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回地址详情数据
	 */
	static async getAddressDetail(params = {}) {
		const { id, showLoading = false } = params;

		if (!id) {
			return {
				success: false,
				data: null,
				message: '地址ID不能为空'
			};
		}

		try {
			const response = await request.authGet('/v1/address/detail', {
				showLoading,
				data: {
					id
				}
			});

			if (response.code === 1) {
				return {
					success: true,
					data: response.data,
					message: response.msg || '获取地址详情成功'
				};
			} else {
				return {
					success: false,
					data: null,
					message: response.msg || '获取地址详情失败'
				};
			}
		} catch (error) {
			console.error('获取地址详情失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '获取地址详情失败'
			};
		}
	}

	/**
	 * 添加地址
	 * @param {Object} params 地址参数
	 * @param {string} params.name 收货人姓名
	 * @param {string} params.phone 收货人电话
	 * @param {string} params.province 省份
	 * @param {string} params.city 城市
	 * @param {string} params.district 区县
	 * @param {string} params.detail 详细地址
	 * @param {boolean} params.isDefault 是否默认地址
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回添加结果
	 */
	static async addAddress(params = {}) {
		const { 
			name, 
			phone, 
			province, 
			city, 
			district, 
			detail, 
			isDefault = false,
			showLoading = false 
		} = params;


		// 参数验证
		if (!name || !phone || !province || !city || !district || !detail) {
			return {
				success: false,
				data: null,
				message: '请填写完整的地址信息'
			};
		}

		try {
			const response = await request.authPost('/v1/address/add', {
				name,
				phone,
				province,
				city,
				district,
				detail,
				is_default: isDefault ? 1 : 0
			}, {
				showLoading
			});

			if (response.code === 1) {
				return {
					success: true,
					data: response.data,
					message: response.msg || '添加地址成功'
				};
			} else {
				return {
					success: false,
					data: null,
					message: response.msg || '添加地址失败'
				};
			}
		} catch (error) {
			console.error('添加地址失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '添加地址失败'
			};
		}
	}

	/**
	 * 更新地址
	 * @param {Object} params 地址参数
	 * @param {number} params.id 地址ID
	 * @param {string} params.name 收货人姓名
	 * @param {string} params.phone 收货人电话
	 * @param {string} params.province 省份
	 * @param {string} params.city 城市
	 * @param {string} params.district 区县
	 * @param {string} params.detail 详细地址
	 * @param {boolean} params.isDefault 是否默认地址
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回更新结果
	 */
	static async updateAddress(params = {}) {
		const { 
			id,
			name, 
			phone, 
			province, 
			city, 
			district, 
			detail, 
			isDefault = false,
			showLoading = false 
		} = params;

		// 参数验证
		if (!id) {
			return {
				success: false,
				data: null,
				message: '地址ID不能为空'
			};
		}

		if (!name || !phone || !province || !city || !district || !detail) {
			return {
				success: false,
				data: null,
				message: '请填写完整的地址信息'
			};
		}

		try {
			const response = await request.authPost('/v1/address/update', {
				id,
				name,
				phone,
				province,
				city,
				district,
				detail,
				is_default: isDefault ? 1 : 0
			}, {
				showLoading
			});

			if (response.code === 1) {
				return {
					success: true,
					data: response.data,
					message: response.msg || '更新地址成功'
				};
			} else {
				return {
					success: false,
					data: null,
					message: response.msg || '更新地址失败'
				};
			}
		} catch (error) {
			console.error('更新地址失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '更新地址失败'
			};
		}
	}

	/**
	 * 删除地址
	 * @param {Object} params 参数
	 * @param {number} params.id 地址ID
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回删除结果
	 */
	static async deleteAddress(params = {}) {
		const { id, showLoading = false } = params;

		if (!id) {
			return {
				success: false,
				data: null,
				message: '地址ID不能为空'
			};
		}

		try {
			const response = await request.authPost('/v1/address/delete', {
				id
			}, {
				showLoading
			});

			if (response.code === 1) {
				return {
					success: true,
					data: response.data,
					message: response.msg || '删除地址成功'
				};
			} else {
				return {
					success: false,
					data: null,
					message: response.msg || '删除地址失败'
				};
			}
		} catch (error) {
			console.error('删除地址失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '删除地址失败'
			};
		}
	}

	/**
	 * 设置默认地址
	 * @param {Object} params 参数
	 * @param {number} params.id 地址ID
	 * @param {boolean} params.showLoading 是否显示加载提示，默认false
	 * @returns {Promise<Object>} 返回设置结果
	 */
	static async setDefaultAddress(params = {}) {
		const { id, showLoading = false } = params;

		if (!id) {
			return {
				success: false,
				data: null,
				message: '地址ID不能为空'
			};
		}

		try {
			const response = await request.authPost('/v1/address/setDefault', {
				id
			}, {
				showLoading
			});

			if (response.code === 1) {
				return {
					success: true,
					data: response.data,
					message: response.msg || '设置默认地址成功'
				};
			} else {
				return {
					success: false,
					data: null,
					message: response.msg || '设置默认地址失败'
				};
			}
		} catch (error) {
			console.error('设置默认地址失败:', error);
			return {
				success: false,
				data: null,
				message: error.message || '设置默认地址失败'
			};
		}
	}
}

module.exports = AddressService; 